package com.weipu.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.weipu.common.DXApplication;
import com.weipu.common.constants.Constant;
import com.weipu.common.facade.model.BannerModel;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.facade.model.NewsCornerModel;
import com.weipu.common.facade.model.ProductModel;

/*
 * 文 件    名:  GlobalConfig.java
 * 创建日期:  2012-9-28下午4:48:17
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  pqp66011
 */

/** 
 * 程序运行的全局变量
 * @version [版本号, 2012-9-28]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author pqp66011
 */
public class GlobalConfig
{
    
    /** 
     * 全局配置单实例  唯一的实例
     */
    private static GlobalConfig instance = new GlobalConfig();
    
    //缓存数据--begin 以下缓存数据出一级栏目外其它类型缓存数据使用动态调整(高内存：内存缓存>>本地数据库缓存；低内存：本地数据库缓存)
    /**
     * 首页挂牌产品缓存对象，
     * 按挂牌类型划分产品列表
     * key: 有UI指定
     */
    private Map<String, ArrayList<ProductModel>> topProductCache;
    
    /**
     *栏目(除一级目录)数据缓存象 
     *父目录ID-->子目录列表对象 
     */
    private Map<String, ArrayList<CatelogModel>> subCategoryListCache;
    
    /**
     *挂牌数据缓存象，见{@link }}
     */
    private ArrayList<BannerModel> bannerCache;
    
    /**
     * 栏目对象数据缓存
     * 栏目ID-->栏目对象
     */
    private Map<String, CatelogModel> categoryMapping;
    
    /**
     * News Corner 缓存
     */
    private ArrayList<NewsCornerModel> newsCache;
    
    /**
                 * 每日消息
     */
    private String messageOfDayCache;
    
    //缓存数据--end
    
    public Map<String, CatelogModel> getCategoryMapping()
    {
        return categoryMapping;
    }
    
    private UserSessionCfg userSessionCfg;
    
    private ArrayList<Integer> rootCategoryIDList;
    
    private Map<Integer, Bitmap> mapImageNor, mapImageHl;
    
    private boolean isCheckSSL = true;
    
    public boolean isCheckSSL() {
		return isCheckSSL;
	}

	public void setCheckSSL(boolean isCheckSSL) {
		this.isCheckSSL = isCheckSSL;
	}
    
    public Map<Integer, Bitmap> getMapImageNor()
    {
        return mapImageNor;
    }
    
    public Map<Integer, Bitmap> getMapImageHl()
    {
        return mapImageHl;
    }
    
    public ArrayList<Integer> getRootCategoryIDList()
    {
        return rootCategoryIDList;
    }
    
    /*设置成private 不让其他程序通过 调用构造器来new 一个对象； 只能通过 GlobalConfig.getInstance()来获取这个配置的对象 */
    private GlobalConfig()
    {
        //用户登录信息
        userSessionCfg = new UserSessionCfg();
        
        //        199: electronics
        //        299: iphone
        //        399: computer
        //        499: tools
        //        599: phone
        //        699: games
        //        799: car
        //        899: toys
        //        999: flashlights
        //        1099: garden
        //        1299: apparel
        //        1499: laptops
        //        1599: photo
        //        1699: sports
        //        1799: health
        //        100099: map
        //根目录信息
        rootCategoryIDList = new ArrayList<Integer>(16);
        rootCategoryIDList.add(199);
        rootCategoryIDList.add(299);
        rootCategoryIDList.add(399);
        rootCategoryIDList.add(499);
        rootCategoryIDList.add(599);
        rootCategoryIDList.add(699);
        rootCategoryIDList.add(799);
        rootCategoryIDList.add(899);
        rootCategoryIDList.add(999);
        rootCategoryIDList.add(1099);
        rootCategoryIDList.add(1299);//P
        rootCategoryIDList.add(149);//p
        rootCategoryIDList.add(159);
        rootCategoryIDList.add(169);
        rootCategoryIDList.add(179);//p
        rootCategoryIDList.add(199);
        rootCategoryIDList.add(100099);//p

        
        setImage();
        
        //        //一级栏目不超过20个
        //        topCategories = new HashMap<String, ArrayList<CatelogModel>>(20);
        
        //4个挂牌，每个缓存数据12条
        topProductCache = new HashMap<String, ArrayList<ProductModel>>(16 * 4);
        
        //非一级栏目缓存，缓存数量可配置(CacheUtil)
        //二级栏目数据不超过30个
        //三级栏目类型不超过20个
        subCategoryListCache = new HashMap<String, ArrayList<CatelogModel>>(20 * 30 * 20);
        
        //一级栏目 
        //二级栏目
        categoryMapping = new HashMap<String, CatelogModel>(20 * 31);
        
        //首页Banner 广告上限5个
        bannerCache = new ArrayList<BannerModel>(Constant.MAIN_BANNER_SIZE);
        
        //News Corner
        newsCache = new ArrayList<NewsCornerModel>(20);
        
    }
    
    /** 
     * 初始化一级目录图片配置
     * @see [类、类#方法、类#成员]
     */
    private void setImage()
    {
        mapImageNor = new HashMap<Integer, Bitmap>();
        mapImageNor.put(199, getImage("ic_categories_electronics_nor.png"));
        mapImageNor.put(299, getImage("ic_categories_iphone_nor.png"));
        mapImageNor.put(399, getImage("ic_categories_computer_nor.png"));
        mapImageNor.put(499, getImage("ic_categories_tools_nor.png"));
        mapImageNor.put(599, getImage("ic_categories_phone_nor.png"));
        mapImageNor.put(699, getImage("ic_categories_games_nor.png"));
        mapImageNor.put(799, getImage("ic_categories_car_nor.png"));
        mapImageNor.put(899, getImage("ic_categories_toys_nor.png"));
        mapImageNor.put(999, getImage("ic_categories_flashlights_nor.png"));
        mapImageNor.put(1099, getImage("ic_categories_garden_nor.png"));
        mapImageNor.put(1299, getImage("ic_categories_apparel_nor.png"));
        mapImageNor.put(1499, getImage("ic_categories_laptops_nor.png"));
        mapImageNor.put(1599, getImage("ic_categories_photo_nor.png"));
        mapImageNor.put(1699, getImage("ic_categories_sports_nor.png"));
        mapImageNor.put(1799, getImage("ic_categories_health_nor.png"));
        mapImageNor.put(100099, getImage("ic_categories_map_nor.png"));
        mapImageNor.put(1399, getImage("ic_categories_flashlights_nor.png"));

        
        mapImageHl = new HashMap<Integer, Bitmap>();
        mapImageHl.put(Constant.CategoryID.CATEGORIES_ELECTRONICS, getImage("ic_categories_electronics_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_IPHONE, getImage("ic_categories_iphone_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_COMPUTER, getImage("ic_categories_computer_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_TOOLS, getImage("ic_categories_tools_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_PHONE, getImage("ic_categories_phone_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_GAMES, getImage("ic_categories_games_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_CAR, getImage("ic_categories_car_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_TOYS, getImage("ic_categories_toys_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_FLASHLIGHTS_999, getImage("ic_categories_flashlights_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_GARDEN, getImage("ic_categories_garden_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_APPAREL, getImage("ic_categories_apparel_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_LAPTOPS, getImage("ic_categories_laptops_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_PHOTO, getImage("ic_categories_photo_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_SPORTS, getImage("ic_categories_sports_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_HEALTH, getImage("ic_categories_health_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_MAP, getImage("ic_categories_map_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_FLASHLIGHTS, getImage("ic_categories_flashlights_hl.png"));
        mapImageHl.put(Constant.CategoryID.CATEGORIES_BODYSHOP, getImage("ic_categories_bodyshop_hl.png"));
        //US仓版本修改 设置新分类USWarehouse的icon的id
        mapImageHl.put(Constant.CategoryID.CATEGORIES_USWAREHOUSE, getImage("ic_categories_uswarehouse.png"));

    }
    
    /**  
     * 获取一级目录的图片
     * @param imageName 本地目录图片名
     * @return
     * @see [类、类#方法、类#成员]
     */
    private Bitmap getImage(String imageName)
    {
        Bitmap image = null;
        AssetManager am = DXApplication.getContext().getResources().getAssets();
        try
        {
            InputStream is = am.open(imageName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        
        return image;
    }
    
    public String getMessageOfDayCache()
    {
        return messageOfDayCache;
    }
    
    public void setMessageOfDayCache(String messageOfDayCache)
    {
        this.messageOfDayCache = messageOfDayCache;
    }
    
    //    public Map<String, ArrayList<CatelogModel>> getTopCategories()
    //    {
    //        return topCategories;
    //    }
    
    public Map<String, ArrayList<CatelogModel>> getSubCategoryList()
    {
        return subCategoryListCache;
    }
    
    public ArrayList<BannerModel> getBannerCache()
    {
        return bannerCache;
    }
    
    public Map<String, ArrayList<ProductModel>> getTopProductCache()
    {
        return topProductCache;
    }
    
    public ArrayList<NewsCornerModel> getNewsCache()
    {
        return newsCache;
    }
    
    /**
     * 获取 GlobalConfig 对象  使用的是单例模式 整个程序中只容许出现一个 GlobalConfig 对象
      * @return GlobalConfig 全局唯一的  GlobalConfig 对象
      * @see [类、类#方法、类#成员]
     */
    public static GlobalConfig getInstance()
    {
        return instance;
    }
    
    public class UserSessionCfg
    {
        UserSessionCfg()
        {
        }
        
        /**
         * 清除用户配置信息
         */
        public void clear()
        {
            customerid = null;
            username = null;
            email = null;
            
            android = null;
            device_model = null;
            version_sdk = null;
            version_release = null;
            MAC = null;
            IP = null;
        }
        
        //用户唯一标识
        private String customerid;
        
        //username
        private String username;
        
        private String token;
        
        private String email;
        
        private String android;
        
        private String device_model;
        
        private String version_sdk;
        
        private String version_release;
        
        private String MAC;
        
        private String IP;
        
        // 屏幕宽度（像素）
        private int width;
        
        // 屏幕高度（像素）
        private int height;
        
        // 屏幕密度（0.75 / 1.0 / 1.5）
        private float density;
        
        // // 屏幕密度DPI（120 / 160 / 240）
        private int densityDpi;
        
        public String getAndroid()
        {
            return android;
        }
        
        public void setAndroid(String android)
        {
            this.android = android;
        }
        
        public String getDevice_model()
        {
            return device_model;
        }
        
        public int getWidth()
        {
            return width;
        }
        
        public void setWidth(int width)
        {
            this.width = width;
        }
        
        public int getHeight()
        {
            return height;
        }
        
        public void setHeight(int height)
        {
            this.height = height;
        }
        
        public float getDensity()
        {
            return density;
        }
        
        public void setDensity(float density)
        {
            this.density = density;
        }
        
        public int getDensityDpi()
        {
            return densityDpi;
        }
        
        public void setDensityDpi(int densityDpi)
        {
            this.densityDpi = densityDpi;
        }
        
        public void setDevice_model(String device_model)
        {
            this.device_model = device_model;
        }
        
        public String getVersion_sdk()
        {
            return version_sdk;
        }
        
        public void setVersion_sdk(String version_sdk)
        {
            this.version_sdk = version_sdk;
        }
        
        public String getVersion_release()
        {
            return version_release;
        }
        
        public void setVersion_release(String version_release)
        {
            this.version_release = version_release;
        }
        
        public String getMAC()
        {
            return MAC;
        }
        
        public void setMAC(String mAC)
        {
            MAC = mAC;
        }
        
        public String getIP()
        {
            return IP;
        }
        
        public void setIP(String iP)
        {
            IP = iP;
        }
        
        public String getEmail()
        {
            return email;
        }
        
        public void setEmail(String email)
        {
            this.email = email;
        }
        
        public String getToken() {
			return token;
		}

		public void setToken(String token) {
			this.token = token;
		}

		public String getCustomerid()
        {
            return customerid;
        }
        
        public void setCustomerid(String customerid)
        {
            this.customerid = customerid;
        }
        
        public String getUsername()
        {
            return username;
        }
        
        public void setUsername(String username)
        {
            this.username = username;
        }
        
    }
    
    public UserSessionCfg getUserSessionCfg()
    {
        return userSessionCfg;
    }
    
    public void setUserSessionCfg(UserSessionCfg userSessionCfg)
    {
        this.userSessionCfg = userSessionCfg;
    }
    
}
